package org.NooLab.texxplugg.tika;


import org.NooLab.utilities.objects.StringedObjects;
import org.NooLab.utilities.strings.StringsUtil;



class TikaWrapper {

	String documentSourceUrl = "";
	String resultText = "" ;
	String xMetaData=""; // most important MetaData as XML
	
	String mimeValue = "" ;
	private int resultType=0;
	
	
	// ========================================================================
	public TikaWrapper(){
		
	}
	// ========================================================================	
	
	public void setsource(String srcUrl) {
		
		documentSourceUrl = srcUrl;

	}

	public void perform() {
	
		TikaExtraction extraction = new TikaExtraction(documentSourceUrl);
		
		try {
			
			resultText = extraction.run();
			
			resultText = StringsUtil.replaceall(resultText, "  ", " ");
			resultText = StringsUtil.replaceall(resultText, "\t", " ");

			xMetaData = extraction.getMeta();
		
			
			mimeValue = extraction.getMimeType();

			if (resultType>=1){
				// create a combined XML
				resultText = produceResultsAsXml(resultText,xMetaData);
			}

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	
	private String produceResultsAsXml(String rtext, String xMetaStr) {

		String resultText ,enctext="";
		StringedObjects so = new StringedObjects();
		
		enctext = so.encode(rtext);
		
		
		resultText = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n   <tikaresults>";
		
		resultText = resultText + "<text>"+ enctext;
		
		resultText = resultText + "</text>";
		
		resultText = resultText + "\n"+ xMetaStr;
		
		resultText = resultText + "\n   </tikaresults>";
		
		so=null;
		return resultText ;
	}

	public String getText(){
		
		return resultText;
	}

	public String getxMetaData() {
		return xMetaData;
	}

	public void setResultFormat(int rtype) {
		
		resultType = rtype;
	}
}
